<?php
#BEGIN_LICENSE
#-------------------------------------------------------------------------
# Module: CGBlog (c) 2010 by Robert Campbell 
#         (calguy1000@cmsmadesimple.org)
#  An addon module for CMS Made Simple to allow creation, management of
#  and display of blog articles.
# 
#  This module forked from the original CMSMS News Module (c) 
#  Ted Kulp, and Robert Campbell.
# 
#-------------------------------------------------------------------------
# CMS - CMS Made Simple is (c) 2005 by Ted Kulp (wishy@cmsmadesimple.org)
# This project's homepage is: http://www.cmsmadesimple.org
#
#-------------------------------------------------------------------------
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# However, as a special exception to the GPL, this software is distributed
# as an addon module to CMS Made Simple.  You may not use this software
# in any Non GPL version of CMS Made simple, or in any version of CMS
# Made simple that does not indicate clearly and obviously in its admin 
# section that the site was built with CMS Made simple.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
# Or read it online: http://www.gnu.org/licenses/licenses.html#GPL
#
#-------------------------------------------------------------------------
#END_LICENSE
if( !isset($gCms) ) return;
if (!$this->CheckPermission('Modify CGBlog'))
  {
    echo $this->ShowErrors($this->Lang('needpermission', array('Modify CGBlog')));
    return;
  }

$this->SetCurrentTab('categories');
if( !isset($params['sub']) || !isset($params['cid']) )
  {
    $this->SetError($this->Lang('error_insufficient_params'));
    $this->RedirectToTab($id);
  }
if( isset($params['catname']) && $params['cid'] != -1 )
  {
    $this->SetError($this->Lang('error_insufficient_params'));
    $this->RedirectToTab($id);
  }
$cid = (int)$params['cid'];

switch( $params['sub'] )
  {
  case 'add':
    {
      $name = trim($params['catname']);
      if(empty($name))
	      {
     	  $this->SetError($this->Lang('error_nocatname'));
	      $this->RedirectToTab($id);
	      }
          else{
      $query = 'SELECT id FROM '.cms_db_prefix().'module_cgblog_categories WHERE name = ? LIMIT 1';
      $tmp = $db->GetOne($query,array($name));
      if( $tmp )
	{
	  $this->SetError($this->Lang('category_name_exists'));
	  $this->RedirectToTab($id);
	}
      $query = 'SELECT max(sort_order) FROM '.cms_db_prefix().'module_cgblog_categories';
      $tmp = $db->GetOne($query);
      if( !$tmp )
	{
	  $tmp = 0;
	}
      $tmp++;
      $query = 'INSERT INTO '.cms_db_prefix().'module_cgblog_categories (name,sort_order) VALUES (?,?)';
      $db->Execute($query,array($name,$tmp));
      $this->SetMessage($this->Lang('category_added'));
      $this->RedirectToTab($id);
          }
    }
    break;

  case 'del':
    {
      // todo first check if there are any articles that are only USING this category';
      // get the info for this category
      $query = 'SELECT sort_order FROM '.cms_db_prefix().'module_cgblog_categories WHERE id = ?';
      $sort_order = $db->GetOne($query,array($cid));
      if( !$sort_order )
	{
	  $this->SetError($this->Lang('error_dberror'));
	  $this->RedirectToTab($id);
	}

      $query = 'DELETE FROM '.cms_db_prefix().'module_cgblog_blog_categories WHERE category_id = ?';
      $db->Execute($query,array($cid));
      $query = 'DELETE FROM '.cms_db_prefix().'module_cgblog_categories WHERE id = ?';
      $db->Execute($query,array($cid));
      $query = 'UPDATE '.cms_db_prefix().'module_cgblog_categories SET sort_order=sort_order-1 WHERE sort_order > ?';
      $db->Execute($query,array($sort_order));

      $this->SetMessage($this->Lang('category_deleted'));
      $this->RedirectToTab($id);
    }
    break;

  case 'mup':
    {
      $query = 'SELECT sort_order FROM '.cms_db_prefix().'module_cgblog_categories WHERE id = ?';
      $old_order = $db->GetOne($query,array($cid));
      
      $query = 'UPDATE '.cms_db_prefix().'module_cgblog_categories SET sort_order=sort_order+1 WHERE sort_order = ?';
      $dbr = $db->Execute($query,array($old_order-1));
      
      $query = 'UPDATE '.cms_db_prefix().'module_cgblog_categories SET sort_order=sort_order-1 WHERE id = ?';
      $db->Execute($query,array($cid));

      $this->RedirectToTab($id);
    }
    break;

  case 'mdown':
    {
      $query = 'SELECT sort_order FROM '.cms_db_prefix().'module_cgblog_categories WHERE id = ?';
      $old_order = $db->GetOne($query,array($cid));
      
      $query = 'UPDATE '.cms_db_prefix().'module_cgblog_categories SET sort_order=sort_order-1 WHERE sort_order = ?';
      $db->Execute($query,array($old_order+1));
      
      $query = 'UPDATE '.cms_db_prefix().'module_cgblog_categories SET sort_order=sort_order+1 WHERE id = ?';
      $db->Execute($query,array($cid));

      $this->RedirectToTab($id);
    }
    break;

  case 'edit':
    {
      $query = 'SELECT * FROM '.cms_db_prefix().'module_cgblog_categories WHERE id = ?';
      $category = $db->GetRow($query,array($cid));

      if( isset($params['cancel']) )
	{
	  $this->RedirectToTab($id);
	}
      else if( isset($params['submit']) )
	{
	  $new_name = trim($params['new_name']);
	  if( empty($new_name) )
	    {
	      echo $this->ShowErrors($this->Lang('error_nocatname'));
	    }
	  else 
	    {
	      // check to make sure it doesn't exist already
	      $query = 'SELECT id FROM '.cms_db_prefix().'module_cgblog_categories 
                         WHERE name = ? AND id != ?';
	      $tmp = $db->GetOne($query,array($new_name,$cid));
	      if( $tmp )
		{
		  // oops
		  echo $this->ShowErrors($this->Lang('category_name_exists'));
		}
	      else
		{
		  // good to go.
		  $query = 'UPDATE '.cms_db_prefix().'module_cgblog_categories
                               SET name= ?
                             WHERE id = ?';
		  $db->Execute($query,array($new_name,$cid));

		  $this->SetMessage($this->Lang('category_modified'));
		  $this->RedirectToTab($id);
		}
	    }
	}
      $smarty->assign('category',$category);
      $smarty->assign('formstart',$this->CGCreateFormStart($id,'admin_editcategory',$returnid,$params));
      $smarty->assign('formend',$this->CreateFormEnd());
      echo $this->ProcessTemplate('admin_editcategory.tpl');
    }
    break;

  case 'dflt':
    $this->SetPreference('default_category',$cid);
    $this->RedirectToTab($id);
    break;
  }
?>